#include "iot_server.h"


void dev_op(int dev, int on)
{
	if (dev == 1) {
		if (on == 1) {
			printf("led2 green on==\n");
			system("echo 1 > /sys/bus/platform/devices/gpio-leds/gpio_led2_green");
		}
		else if (on == 0) {
			printf("led2 green off==\n");
			system("echo 0 > /sys/bus/platform/devices/gpio-leds/gpio_led2_green");

		}
	}
	if (dev == 2) {
		if (on == 1) {
			printf("led2 orange on==\n");
			system("echo 1 > /sys/bus/platform/devices/gpio-leds/gpio_led2_orange");

		} else if (on == 0) {
			printf("led2 orange off==\n");
			system("echo 0 > /sys/bus/platform/devices/gpio-leds/gpio_led2_orange");
		}
	}

}


//从zigbee命令队列出队节点并发送给zigbee模块终端线程
void * out_cmd_thread(void *arg)
{
	int cnt = 0;
	int dev = -1, on = -1;
	char cmdstr[50]; //保存发给zigbee终端的原始命令字符串
	char devname[30];
	char op[30];
	while (1) {
		pthread_mutex_lock(&cmd_queue_mutex);
		pthread_cond_wait(&cmd_queue_cond, &cmd_queue_mutex); //等待接收命令节点唤醒
		memset(cmdstr, 0, sizeof(cmdstr));
		if(linkqueue_cmdstr_out(pcmdque, cmdstr)) {
			printf("********************************\n");
			cnt++;
			printf("$$$命令链表队列节点%d 出队命令字符串:$$$\n ", cnt);
			printf("%s\n", cmdstr);
			printf("********************************\n\n");
		}
		pthread_mutex_unlock(&cmd_queue_mutex);
		dev = -1;
		on = -1;
		sscanf(cmdstr, "%s%s", devname, op);
		printf("[%s] [%s]\n", devname, op);
		if (strncmp(devname, "led2_green", strlen(devname)) == 0) dev = 1;
		if (strncmp(devname, "led2_orange", strlen(devname)) == 0) dev = 2;
		
		if (strncmp(op, "on", strlen(op)) == 0) on = 1;
		if (strncmp(op, "off", strlen(op)) == 0) on = 0;




		if ((dev != -1 ) && (on != -1)) {
			dev_op(dev, on);
			do_cmds(cmdstr, db);
		
		}



		/**
		 * 		//control mac one one
		 * 获取实现上面单拍、多拍功能 指令，写入管道给web服务器进程
		 * 让web服务器进程来实现单拍、多拍功能
		 */
		if ((strncmp(devname, "one", strlen(devname)) == 0) && 
			(strncmp(op, "one", strlen(op)) == 0) ) {
		 	system("echo one > /tmp/webpipe");
            do_cmds(cmdstr, db);   
		}

		if ((strncmp(devname, "five", strlen(devname)) == 0) && 
			(strncmp(op, "five", strlen(op)) == 0) ) {
			system("echo five > /tmp/webpipe");
			do_cmds(cmdstr, db); 

		}
		

		
		
		
	}
			
	return NULL;

}

